package com.pronhub.luowowo.article.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pronhub.luowowo.article.domain.*;
import com.pronhub.luowowo.article.mapper.StrategyContentMapper;
import com.pronhub.luowowo.article.mapper.StrategyDetailMapper;
import com.pronhub.luowowo.article.query.StrategyDetailQuery;
import com.pronhub.luowowo.article.service.IDestinationService;
import com.pronhub.luowowo.article.service.IStrategyDetailService;
import com.pronhub.luowowo.article.service.IStrategyService;
import com.pronhub.luowowo.article.service.IStrategyTagService;
import com.pronhub.luowowo.article.vo.StrategyPersistenceVO;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        // 目的地的id   根据攻略的id来获取dest_id 用到他的方法
        Strategy strategy = strategyService.get(strategyDetail.getStrategy().getId());
        Destination dest = strategy.getDest();
        strategyDetail.setDest(dest);
        //作者的id  暂时设置为1

        //摘要暂时是我们写 随便写一点 在真实中,一般由运维人员进行描写 随便搞一搞
        String content = strategyDetail.getStrategyContent().getContent();
        String summary = "";
        if (content != null && content.length() > 200) {
            summary = content.substring(0, 200);
            strategyDetail.setSummary(summary);
        } else {
            strategyDetail.setSummary(summary);
        }

        //创建时间必须是新增的时候 所以放到添加的时候
        //是否是国外  根据的地点id 然后得到吐司 最爷爷那个id是否等于1  如果是 就是国内
        List<Destination> toasts = destinationService.getToasts(dest.getId());
        Destination destinationOftoasts = toasts.get(0);
        strategyDetail.setIsabroad(destinationOftoasts.getId() != 1);


        //传过来没有地区的id 但是有攻略的id  就是通过攻略的的id去查到是哪个地区的
        if (strategyDetail.getId() != null) {
            // -------------------更新-----------------------
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            //内容的处理
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }else {
            //-------------------新加-----------------------
            //添加时间
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);


            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);
        }

        //-----------------------------开始便签的添加还有更改
        //这个时候上面已经有了id  可以往里面进行添加操作
        String[] split = tags.split(",");
        for (String s : split) {
            StrategyTag strategyTag = new StrategyTag();
            strategyTag.setName(s);
            strategyTagService.saveOrUpdate(strategyTag);
            //维护关系
            strategyDetailMapper.increaseTheRelation(strategyDetail.getId(), strategyTag.getId());
        }
    }

    @Override
    public StrategyContent getContentById(Long id) {
        return  strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyDetail> queryViewTop3(Long destId) {

        return strategyDetailMapper.selectViewTop3(destId);
    }

    @Override
    public void updateDataByRedisVO(StrategyPersistenceVO vo) {
        strategyDetailMapper.updateDataByRedisVO(vo);
    }
}
